﻿using University.Workshop.Business;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using University.Workshop.Model;
using System.Collections.ObjectModel;
using University.Workshop.DataAccess;

namespace University.Workshop.Tests
{
    
    
    /// <summary>
    ///This is a test class for TicketManagerTest and is intended
    ///to contain all TicketManagerTest Unit Tests
    ///</summary>
    [TestClass()]
    public class TicketManagerTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for GetPurchasesByUser
        ///</summary>
        [TestMethod()]
        public void GetPurchasesByUserTest()
        {
            string firstName = "Pedro"; // TODO: Initialize to an appropriate value
            string lastName = "Suarez"; // TODO: Initialize to an appropriate value
            char gender = 'M'; // TODO: Initialize to an appropriate value
            Random rand = new Random();
            DateTime birthday = DateTime.Now.AddYears(-18); // TODO: Initialize to an appropriate value
            string email = "pedro" + rand.Next(400) + "@gmail.com"; // TODO: Initialize to an appropriate value
            string password = "qwert1"; // TODO: Initialize to an appropriate value
            string rePassword = "qwert1"; // TODO: Initialize to an appropriate value
            string department = "Cochabamba"; // TODO: Initialize to an appropriate value
            string city = "Cercado"; // TODO: Initialize to an appropriate value
            string movilephone = "" + rand.Next(79999999); // TODO: Initialize to an appropriate value
            string phone = "" + rand.Next(4999999); // TODO: Initialize to an appropriate value
            UserRegistrationManager.RegisterUser(firstName, lastName, gender, birthday, email, password, rePassword, department, city, movilephone, phone);
            Collection<Product> products = new Collection<Product>();
            long buyerId = UserDataAccess.GetUserByEmail(email).Id;
            Product prod1 = ProductRegistrationManager.RegisterNewProduct("computadoras portatiles", "5234", "fafawfwef awefwfwefawfwef afwefawefawf fasfsafsaffsf", "Electronics", false, "Dollars", buyerId, "1");
            Product prod2 = ProductRegistrationManager.RegisterNewProduct("deportivos americanos", "334", "fafawfwef awefwfwefawfwef afwefawefawf fasfsafsaffsf", "Fashion", false, "Bolivianos", buyerId, "1");
            products.Add(prod1);
            products.Add(prod2);
            long ordId = OrderDataAccess.AddNewOrder(products, buyerId);
            OrderDataAccess.CompleteOrder(ordId);
            Collection<PurchasedProduct> actual;
            actual = TicketManager.GetPurchasesWithoutTicketByUser(email);
            foreach (var item in actual)
            {
                if (products[0].ProductId == item.ProductBought.ProductId)
                    Assert.IsTrue(products[0].ProductId == item.ProductBought.ProductId);
            }
        }

        /// <summary>
        ///A test for SendTicket
        ///</summary>
        [TestMethod()]
        public void SendTicketTest()
        {
            string issueTitle = "problem?"; // TODO: Initialize to an appropriate value
            string description = "trololol"; // TODO: Initialize to an appropriate value
            OrderDataAccessTest ord = new OrderDataAccessTest();
            ord.AddNewOrderTest();
            ord.CompleteOrderTest();
            long orderId = 1; // TODO: Initialize to an appropriate value
            short priority = 4; // TODO: Initialize to an appropriate value
            Ticket expected = new Ticket {OrderId=1,IssueTitle=issueTitle, Description=description }; // TODO: Initialize to an appropriate value
            Ticket actual;
            actual = TicketManager.SendTicket(issueTitle, description, orderId, priority);
            Assert.AreEqual(expected.OrderId, actual.OrderId);
        }

        /// <summary>
        ///A test for GatherHigherPriorityTickets
        ///</summary>
        [TestMethod()]
        public void GatherHigherPriorityTicketsTest()
        {
            TicketDataAccessTest test = new TicketDataAccessTest();
            test.RegisterTicketTest();
            Collection<Ticket> actual;
            actual = TicketManager.GatherHigherPriorityTickets();
            Assert.IsNotNull(actual);
        }

        /// <summary>
        ///A test for GatherNewestTickets
        ///</summary>
        [TestMethod()]
        public void GatherNewestTicketsTest()
        {
            TicketDataAccessTest test = new TicketDataAccessTest();
            test.RegisterTicketTest();
            Collection<Ticket> actual;
            actual = TicketManager.GatherNewestTickets();
            Assert.IsNotNull(actual);
        }

        /// <summary>
        ///A test for GetPurchasesWithoutTicketByUser
        ///</summary>
        [TestMethod()]
        public void GetPurchasesWithoutTicketByUserTest()
        {
            OrderDataAccessTest test = new OrderDataAccessTest();
            test.CompleteOrderTest();
            Collection<PurchasedProduct> actual;
            actual = TicketManager.GetPurchasesWithoutTicketByUser("admin@admin.com");
            Assert.IsNotNull(actual);
        }

        /// <summary>
        ///A test for RespondTicket
        ///</summary>
        [TestMethod()]
        public void RespondTicketTest()
        {
            string firstName = "George"; // TODO: Initialize to an appropriate value
            string lastName = "Cluning"; // TODO: Initialize to an appropriate value
            char gender = 'M'; // TODO: Initialize to an appropriate value
            Random rand = new Random();
            DateTime birthday = DateTime.Now.AddYears(-18); // TODO: Initialize to an appropriate value
            string email = "George" + rand.Next(1000) + "@email.com"; // TODO: Initialize to an appropriate value
            string password = "qwert1"; // TODO: Initialize to an appropriate value
            string rePassword = "qwert1"; // TODO: Initialize to an appropriate value
            string department = "Pando"; // TODO: Initialize to an appropriate value
            string city = "Cercado"; // TODO: Initialize to an appropriate value
            string movilephone = "" + rand.Next(79999999); // TODO: Initialize to an appropriate value
            string phone = "" + rand.Next(4999999); // TODO: Initialize to an appropriate value
            UserRegistrationManager.RegisterUser(firstName, lastName, gender, birthday, email, password, rePassword, department, city, movilephone, phone);
            Collection<Product> products = new Collection<Product>();
            User buyer = UserDataAccess.GetUserByEmail(email);
            Product prod = ProductRegistrationManager.RegisterNewProduct("Samsumg galaxy 3", "200", "New Cellphone New Cellphone New Cellphone New Cellphone New Cellphone New Cellphone", "Electronics", false, "Dollars", buyer.Id, "1");
            products.Add(prod);
            long orderId = OrderDataAccess.AddNewOrder(products, buyer.Id);
            OrderDataAccess.CompleteOrder(orderId);
            var orders = OrderDataAccess.GetOrdersByBuyerId(buyer.Id);
            long idOfOrder = 1;
            foreach (Order order in orders)
            {
                if (order.ProductId == prod.ProductId)
                {
                    idOfOrder = order.Id;
                    break;
                }
            }
            Ticket ticket = TicketDataAccess.RegisterTicket(new Ticket { Date = DateTime.Now, Priority = 4, Status = TicketStatus.New, IssueTitle = "other Ticket", Description = "this is another ticket for test", OrderId = idOfOrder });
            long ticketId = ticket.Id; // TODO: Initialize to an appropriate value
            string userEmail = buyer.ContactInformation.Email; // TODO: Initialize to an appropriate value
            string reply = "This is a reply for your ticket"; // TODO: Initialize to an appropriate value
            TicketReply actual;
            actual = TicketManager.RespondTicket(ticketId, userEmail, reply);
            Assert.AreEqual(ticketId, actual.TicketId);
        }

        /// <summary>
        ///A test for UserIsTheOwnerOfTicket
        ///</summary>
        [TestMethod()]
        public void UserIsTheOwnerOfTicketTest()
        {
            string firstName = "George"; // TODO: Initialize to an appropriate value
            string lastName = "Cluning"; // TODO: Initialize to an appropriate value
            char gender = 'M'; // TODO: Initialize to an appropriate value
            Random rand = new Random();
            DateTime birthday = DateTime.Now.AddYears(-18); // TODO: Initialize to an appropriate value
            string email = "George" + rand.Next(1000) + "@email.com"; // TODO: Initialize to an appropriate value
            string password = "qwert1"; // TODO: Initialize to an appropriate value
            string rePassword = "qwert1"; // TODO: Initialize to an appropriate value
            string department = "Pando"; // TODO: Initialize to an appropriate value
            string city = "Cercado"; // TODO: Initialize to an appropriate value
            string movilephone = "" + rand.Next(79999999); // TODO: Initialize to an appropriate value
            string phone = "" + rand.Next(4999999); // TODO: Initialize to an appropriate value
            UserRegistrationManager.RegisterUser(firstName, lastName, gender, birthday, email, password, rePassword, department, city, movilephone, phone);
            Collection<Product> products = new Collection<Product>();
            User buyer = UserDataAccess.GetUserByEmail(email);
            Product prod = ProductRegistrationManager.RegisterNewProduct("Samsumg galaxy 2", "120", "New Cellphone New Cellphone New Cellphone New Cellphone New Cellphone New Cellphone", "Electronics", false, "Dollars", buyer.Id, "1");
            products.Add(prod);
            long orderId = OrderDataAccess.AddNewOrder(products, buyer.Id);
            OrderDataAccess.CompleteOrder(orderId);
            var orders = OrderDataAccess.GetOrdersByBuyerId(buyer.Id);
            long idOfOrder = 1;
            foreach (Order order in orders)
            {
                if (order.ProductId == prod.ProductId)
                {
                    idOfOrder = order.Id;
                    break;
                }
            }
            Ticket ticket = TicketDataAccess.RegisterTicket(new Ticket { Date = DateTime.Now, Priority = 4, Status = TicketStatus.New, IssueTitle = "other Ticket for other test", Description = "this is another ticket for test", OrderId = idOfOrder });
            long ticketId = 1; // TODO: Initialize to an appropriate value
            bool actual;
            actual = TicketManager.UserIsTheOwnerOfTicket(email, ticketId);
            Assert.AreEqual(false, actual);
        }
    }
}
